<template>
  <div class="flex_cm container" style="height: 100%">
    <search
      :data="header_list"
      @changeIsShow="
        (e) => {
          header_list[e].isShow = !header_list[e].isShow
          header_list[e].value =
            header_list[e].type == 'input_number' ? ['', ''] : ''
        }
      "
      @search="search_key"
      @reset="
        header_list.forEach((item) => {
          item.value = ''
        }),
          search_key()
      "
    ></search>
    <div class="p_0_10 bg_fff flex1">
      <div class="flex_js p_10_0">
        <div class="c_666 fw">平台用户</div>
        <div class="flex">
          <!-- <el-button type="primary" @click="show_box = true">新增</el-button> -->

          <setTable
            :data="header_list"
            :height_size="height_size"
            @changeShowTable="
              (e) => {
                header_list[e].showTable = !header_list[e].showTable
              }
            "
            @resetTableSet="
              () => {
                header_list.forEach((item) => {
                  item.showTable = true
                })
              }
            "
            @changeHeightSize="
              (e) => {
                height_size = e
              }
            "
          ></setTable>
        </div>
      </div>
      <el-table
        style="text-align: center"
        :data="list"
        :max-height="max_height"
        border
        highlight-current-row
        v-loading="loading"
        :default-sort="{ prop: 'name', order: 'descending' }"
        :row-style="{
          height: height_size == 2 ? '50px' : '',
        }"
        :cell-style="{
          padding: height_size == 3 ? '0' : '',
        }"
      >
        <el-table-column
          type="index"
          label="序号"
          width="80px"
        ></el-table-column>
        <el-table-column
          label="用户名"
          prop="name"
          key="用户名"
          sortable
          v-if="header_list[0].showTable"
        ></el-table-column>
        <el-table-column
          label="昵称"
          prop="nickName"
          key="昵称"
          sortable
          v-if="header_list[1].showTable"
        ></el-table-column>
        <el-table-column
          label="账号"
          prop="phone"
          sortable
          key="账号"
          width="120px"
          v-if="header_list[2].showTable"
        ></el-table-column>
        <el-table-column
          label="微信"
          prop="wechat"
          sortable
          key="微信"
          width="120px"
          v-if="header_list[3].showTable"
        ></el-table-column>
        <el-table-column
          label="拥有企业"
          prop="possessEnterprise"
          sortable
          key="拥有企业"
          v-if="header_list[4].showTable"
        >
        </el-table-column>
        <el-table-column
          label="加入企业"
          key="加入企业"
          sortable
          prop="joinEnterprise"
          v-if="header_list[5].showTable"
        ></el-table-column>
        <el-table-column
          label="最近登录时间"
          key="最近登录时间"
          sortable
          prop="lastLoginTime"
          v-if="header_list[6].showTable"
          width="180px"
        >
          <template #default="scope: any">
            <span v-if="scope.row.lastLoginTime">{{
              formatDate(
                parseInt(scope.row.lastLoginTime),
                'yyyy-MM-dd hh:mm:ss'
              )
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template #default="scope: any">
            <el-button type="primary" @click="reset_pwd(scope.row)"
              >重置密码</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="m_10_0 pagination"
        v-model:currentPage="page_data.current"
        v-model:page-size="page_data.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="list_debounce()"
        @current-change="list_debounce()"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { formatDate, debounce, handle_search_key } from '@/utils/utils' // 权限校验
import search from '@/view/components/search.vue'
import setTable from '@/view/components/setTable.vue'
import { onMounted, ref, onUnmounted, nextTick } from 'vue'
import axios from '@/service/HttpRequest'
import serviceApi from '@/service/api'
import EventBus from '@/utils/EventBus'
import { ElMessageBox, ElMessage } from 'element-plus'
const list = ref()
const total = ref()
const max_height = ref()
nextTick(() => {
  max_height.value = window.innerHeight - 320 // 不带分页的 table 高度
})
const loading = ref(false)
const height_size = ref(1)
const header_list = ref<any>([
  {
    label: '用户名',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '昵称',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '账号',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '微信',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '拥有企业',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '加入企业',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '最近登录时间',
    value: ['', ''],
    showTable: true,
    type: 'date',
    isShow: false,
    options: [],
  },
])
const page_data = ref({
  current: 1,
  pageSize: 10,
  name: '',
  nickName: '',
  account: '',
  wechat: '',
  possessEnterprise: '',
  joinEnterprise: '',
  lastLoginTime: '',
})
onMounted(() => {
  EventBus.on('refresh', () => {
    get_list()
    ElMessage.success('操作成功！')
  })
  get_list()
})
onUnmounted(() => {
  EventBus.off('refresh')
})
const reset_pwd = (e) => {
  ElMessageBox.confirm('确定将该用户密码重置吗？', '提示', {
    cancelButtonText: '取消',
    confirmButtonText: '确定',
    closeOnClickModal: false,
    type: 'warning',
  })
    .then(() => {
      axios
        .request({
          url: serviceApi.reset_pwd,
          method: 'put',
          params: { memberId: e.id },
        })
        .then((res) => {
          console.log(res)
          ElMessage.success('操作成功！')
        })
    })
    .catch(() => {})
}
const get_list = () => {
  axios
    .request({
      url: serviceApi.platform_member_list,
      method: 'post',
      data: handle_search_key(page_data.value),
    })
    .then((res) => {
      console.log(res)
      list.value = res.data !== null ? res.data.content : []
      total.value =
        res.data !== null ? parseInt(res.data.totalElements) : total.value
    })
}
let list_debounce = debounce(200, get_list)
const search_key = () => {
  page_data.value = {
    current: 1,
    pageSize: 10,
    name: header_list.value[0].value,
    nickName: header_list.value[1].value,
    account: header_list.value[2].value,
    wechat: header_list.value[3].value,
    joinEnterprise: header_list.value[5].value,
    possessEnterprise: header_list.value[4].value,
    lastLoginTime: header_list.value[6].value,
  }
  get_list()
}
</script>

<style lang="scss" scoped></style>
